package com.wanlian.dao.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.wanlian.common.PageCondition;
import com.wanlian.dao.OrgRoleGroupDao;
import com.wanlian.entity.OrgRoleGroup;
import com.wanlian.model.PageResult;
import com.wanlian.model.ResultModel;
import com.wanlian.util.DateUtil;
import com.wanlian.util.StringUtil;
import com.wanlian.vo.Page;
import org.springframework.stereotype.Component;


@Component
public class OrgRoleGroupDaoImpl extends GenericDaoImpl<OrgRoleGroup> implements OrgRoleGroupDao
{

    public OrgRoleGroupDaoImpl()
    {
        super(OrgRoleGroup.class);
    }

    @Override
    public void add(OrgRoleGroup t)
    {
        super.add(t);
    }

    @Override
    public void delete(int id)
    {
        OrgRoleGroup group = this.queryById(id);
        if (group == null)
        {
            return;
        }
        List<OrgRoleGroup> list = this.queryForListByWhere(" parentid = " + group.getRoleGroupId(), null);
        for (OrgRoleGroup orgRoleGroup : list)
        {
            orgRoleGroup.setLastupdatetime(DateUtil.currentTimestamp());
            orgRoleGroup.setIsdel(true);
            orgRoleGroup.setIsenable(false);
            this.update(orgRoleGroup);
        }
        group.setLastupdatetime(DateUtil.currentTimestamp());
        group.setIsdel(true);
        group.setIsenable(false);
        this.update(group);
    }

    @Override
    public void doedit(OrgRoleGroup group)
    {
        this.update(group);
    }





    @Override
    public ResultModel queryPageList(PageCondition pageCondition)
    {

        String[] column={"rolegroupid","candel","createtime","isdel","isenable","lastupdatetime","rolegroupname","createname"};
        String sql="select r.rolegroupid,r.candel,r.createtime,r.isdel,r.isenable,r.lastupdatetime,r.rolegroupname,u.username as createname from tb_orgrolegroup r left join  tb_user u on r.createid=u.id  ";

        ArrayList<Object> params = new ArrayList<Object>();
        StringBuffer sqlWhere=new StringBuffer("where isdel=0");
        if (pageCondition.isExistByKey("roleGroupName")){
            String roleGroupName= (String) pageCondition.getConditions().get("roleGroupName");
            if(StringUtil.hasValue(roleGroupName)){
                sqlWhere.append(" and r.rolegroupname like ?");
                params.add("%" +roleGroupName+ "%");
            }

        }
        if(pageCondition.isExistByKey("createName")){
            String createName= (String) pageCondition.getConditions().get("createName");
            if(StringUtil.hasValue(createName)){
                sqlWhere.append(" and u.username like ?");
                params.add("%" +createName+ "%");
            }
        }
        if(pageCondition.isExistByKey("startTime")){
            String startTime= (String) pageCondition.getConditions().get("startTime");
            if(StringUtil.hasValue(startTime)){
                sqlWhere.append("and r.createtime >= ?");
                Timestamp st = Timestamp.valueOf(startTime + " 00:00:00.00");
                params.add(st);
            }
        }
        if(pageCondition.isExistByKey("endTime")){
            String endTime= (String) pageCondition.getConditions().get("endTime");
            if(StringUtil.hasValue(endTime)){
                sqlWhere.append("and r.createtime <= ?");
                Timestamp et = Timestamp.valueOf(endTime + " 23:59:59.00");
                params.add(et);
            }
        }
        List<Map<String, Object>> lists = this.queryForListObjectByPage(sql+sqlWhere.toString(), params.toArray(), pageCondition.getPage(), column);
        int count =this.queryCountByParam("select count(1) from tb_orgrolegroup r left join  tb_user u on r.createid=u.id  "+sqlWhere,params.toArray());
        ResultModel resultModel = new ResultModel(true, lists, count);
        return resultModel;
    }







    @Override
    public OrgRoleGroup queryByName(String name) {
        String hql = String.format("from %s where rolegroupname = ? ", this.entityClassName());
        OrgRoleGroup orgRoleGroup = this.queryForObject(hql, new Object[]{name});
        return orgRoleGroup;
    }


    @Override
    public List<Map<String, Object>> queryGroup(OrgRoleGroup orgRoleGroup) {
        String[] column = {"parentid", "rolegroupid", "rolegroupname"};
        StringBuilder sqlWhere = new StringBuilder();

        sqlWhere.append("   SELECT distinct t.parentid ,t.rolegroupid ,t.rolegroupname FROM  tb_orgrolegroup t,tb_orgrolegroup t1");
        sqlWhere.append(" WHERE  1=1 and  t.isenable =1 ");

        ArrayList<Object> param = new ArrayList<Object>();
       /* if (pageCondition.isEmpty("roleGroupId")) {
            int userId = Integer.parseInt((String) pageCondition.getByKey("roleGroupId"));
            sqlWhere.append(" and roleGroupId=? ");
            param.add(userId);
        }*/
       if(orgRoleGroup!=null&&orgRoleGroup.getRoleGroupId()>0){
           sqlWhere.append(" and t.roleGroupId=? ");
           param.add(orgRoleGroup.getRoleGroupId());
       }

       sqlWhere.append(" and (t.parentid = t1.rolegroupid or  t.parentid =0)");
        sqlWhere.append(" ORDER BY concat(t.parentid, t.rolegroupid)");
        System.out.println(sqlWhere);
        List<Map<String, Object>> lists = new ArrayList<>();
        String hql = String.format( ""+ sqlWhere, entityClassName());
        Page page=new Page(1,9999);
        lists = this.queryForListObjectByPage(hql, param.toArray(), page,column);
        return lists;
    }

    @Override
    public List<OrgRoleGroup> queryByPid(int parentId) {
        List<OrgRoleGroup> lists = new ArrayList<OrgRoleGroup>();
        String hql = String.format("from %s where  parentid = ? and isenable=? ", entityClassName());
        lists = this.queryForList(hql, new Object[]{parentId,true});
        return lists;
    }
}
